package com.gdziarmaga.webshop.allegro.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.IntHolder;
import javax.xml.rpc.holders.LongHolder;
import javax.xml.rpc.holders.StringHolder;

import AllegroWebApi.AllegroWebApiPortType;
import AllegroWebApi.AllegroWebApiService;
import AllegroWebApi.AllegroWebApiServiceLocator;
import AllegroWebApi.FieldsValue;
import AllegroWebApi.RangeDateValueStruct;
import AllegroWebApi.RangeFloatValueStruct;
import AllegroWebApi.RangeIntValueStruct;

import com.gdziarmaga.webshop.book.model.Book;

public class WebshopAllegroAPI {

	public static boolean sellBook(Book book, String serverPath) {

		AllegroWebApiService allegroWebApiService = new AllegroWebApiServiceLocator();
		AllegroWebApiPortType allegroWebApiPort;
		try {
			allegroWebApiPort = allegroWebApiService.getAllegroWebApiPort();
			LongHolder serverTime = new LongHolder();
			StringHolder session = new StringHolder();
			LongHolder userId = new LongHolder();
			allegroWebApiPort.doLogin("grzesiekd58", "qwerty1", 228,
					"9c51cbcf8a", 38191927L, session, userId, serverTime);

			allegroWebApiPort.doGetCountries(228, "9c51cbcf8a");

			// Tytuł
			FieldsValue[] values = new FieldsValue[17];
			FieldsValue value1 = getEmptyField();
			value1.setFid(1);
			value1.setFvalueString(book.getTitle());
			values[0] = value1;

			// Dział na Allgro
			FieldsValue value2 = getEmptyField();
			value2.setFid(2);
			value2.setFvalueInt(1822);
			values[1] = value2;

			// Data rozpoczecia
			FieldsValue value3 = getEmptyField();
			value3.setFid(3);
			Date date = new Date();
			date.setMinutes(date.getMinutes() + 1);
			value3.setFvalueDate(date.toString());
			values[2] = value3;

			// Czas trwania aukcji (dopuszczalne 0|1|2|3|4|5 => 3|5|7|10|14|30
			// dni )
			FieldsValue value4 = getEmptyField();
			value4.setFid(4);
			value4.setFvalueInt(2);
			values[3] = value4;

			// Liczba sztuk (int)
			FieldsValue value5 = getEmptyField();
			value5.setFid(5);
			value5.setFvalueInt(1);
			values[4] = value5;

			// Cena wywolawcza (float)
			FieldsValue value6 = getEmptyField();
			value6.setFid(6);
			// value6.setFvalueFloat(23f);
			value6.setFvalueFloat(book.getPrice() - 0.05f * book.getPrice());
			values[5] = value6;

			// Cena KUP TERAZ (float)
			FieldsValue value16 = getEmptyField();
			value16.setFid(8);
			value16.setFvalueFloat(book.getPrice());
			values[15] = value16;

			// Kraj
			FieldsValue value7 = getEmptyField();
			value7.setFid(9);
			value7.setFvalueInt(228);
			values[6] = value7;

			// Województwo
			FieldsValue value8 = getEmptyField();
			value8.setFid(10);
			value8.setFvalueInt(214);
			values[7] = value8;

			// Miasto
			FieldsValue value9 = getEmptyField();
			value9.setFid(11);
			value9.setFvalueString("Warszawa");
			values[8] = value9;

			// Formy platnosci (dopuszczalne 1|2|4|8 =>
			// Platne z gory (przelew)|-|Szczegoly w opisie|Wystawiam faktury
			// VAT) (możliwość sumowania pól)
			FieldsValue value11 = getEmptyField();
			value11.setFid(14);
			value11.setFvalueInt(1);
			values[10] = value11;

			// Opcje dodatkowe (dopuszczalne 1|2|4|8|16|32|64 =>
			// Pogrubienie|Miniaturka|Podswietlenie|Wyroznienie|Strona
			// kategorii|Strona glowna Allegro|Znak wodny)
			FieldsValue value17 = getEmptyField();
			value17.setFid(15);
			value17.setFvalueInt(3);
			values[9] = value17;

			// Opis
			FieldsValue value12 = getEmptyField();
			value12.setFid(24);
			// value12.setFvalueString("LongDescritpion");
			value12.setFvalueString(book.getLong_descripton());
			values[11] = value12;

			// Format sprzedaży (dopuszczalne 0|1=>
			// Aukcja (z licytacją lub Kup Teraz!|Sklep (bez
			// licytacji)))(domyslnie 0)
			FieldsValue value14 = getEmptyField();
			value14.setFid(29);
			value14.setFvalueInt(0);
			values[13] = value14;

			// Paczka pocztowa ekonomiczna (pierwsza sztuka) (float)
			FieldsValue value13 = getEmptyField();
			value13.setFid(36);
			value13.setFvalueFloat(8f);
			values[12] = value13;

			// Kod pocztowy
			FieldsValue value15 = getEmptyField();
			value15.setFid(32);
			value15.setFvalueString("00-631");
			values[14] = value15;

			// Zdjęcie
			FieldsValue value18 = getEmptyField();
			value18.setFid(16);
			File file = new File(serverPath
					+ "resources\\images\\book_icons\\Java\\big\\"
					+ book.getIsbn() + ".jpg");
			FileInputStream fin = new FileInputStream(file);
			byte fileContent[] = new byte[(int) file.length()];
			fin.read(fileContent);
			value18.setFvalueImage(fileContent);
			values[16] = value18;

			LongHolder longHolder = new LongHolder();
			StringHolder stringHolder = new StringHolder();
			IntHolder intHolder = new IntHolder();

			// ArrayOfSellFormsHolder sellFormFields = new
			// ArrayOfSellFormsHolder();
			// LongHolder longh = new LongHolder();
			// StringHolder string = new StringHolder();
			// allegroWebApiPort.doGetSellFormFieldsExt(228, 38191927L,
			// "9c51cbcf8a", sellFormFields, longh, string);
			allegroWebApiPort.doNewAuctionExt(session.value, values, 1, 2,
					longHolder, stringHolder, intHolder);
			return true;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static FieldsValue getEmptyField() {
		FieldsValue value1 = new FieldsValue();
		value1.setFvalueDate("");
		value1.setFvalueDatetime(0F);
		value1.setFvalueFloat(0F);
		value1.setFvalueInt(0);
		value1.setFvalueImage(new byte[1]);
		RangeDateValueStruct dateValueStruct = new RangeDateValueStruct();
		dateValueStruct.setFvalueRangeDateMax("");
		dateValueStruct.setFvalueRangeDateMin("");
		value1.setFvalueRangeDate(dateValueStruct);
		value1.setFvalueRangeFloat(new RangeFloatValueStruct());
		value1.setFvalueRangeInt(new RangeIntValueStruct());

		value1.setFvalueString("");

		return value1;
	}

}
